import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    nextId: 1,
    list: [],
    inputval: '',
    viewkey: 'all'
  },
  mutations: {
    initlist (state, list) {
      state.list = list
    },
    additem (state) {
      const iteminfo = {
        id: state.nextId++,
        info: state.inputval.trim(),
        done: false
      }
      state.list.push(iteminfo)
      state.inputval = ''
    },
    removeitem (state, id) {
      const i = state.list.findIndex(x => x.id === id)
      state.list.splice(i, 1)
    },
    changeitem (state, params) {
      const i = state.list.findIndex(x => x.id === params.id)
      state.list[i].done = params.status
    },
    showitem (state, status) {
      state.viewkey = status
    },
    cleardnoe (state) {
      state.list = state.list.filter(item => !item.done)
    }
  },
  getters: {
    listinfo (state) {
      if (state.viewkey === 'all') {
        return state.list
      } else if (state.viewkey === 'done') {
        return state.list.filter(item => item.done)
      } else {
        return state.list.filter(item => !item.done)
      }
    },
    unDoneLength (state) {
      return state.list.filter(item => !item.done).length
    }
  }
})
